using System.Collections.Generic;
using UnityEngine;

public class GameplayRewardManager : MonoBehaviour
{
	public class KillStreak
	{
		public RewardMessages reward;

		public int number;

		public string nameStreak;

		public int count;

		public int Number
		{
			get
			{
				return number;
			}
			set
			{
				number = value;
			}
		}
	}

	public static GameplayRewardManager instance;

	public ReputationLevel[] reputationLevels;

	public GameObject GameplayEventMessagePrefab;

	public GameObject NewReputationLevelMessagePrefab;

	public GameObject HiddenCoinCollectedMessagePrefab;

	public GameObject OverheadTextMessagePrefab;

	public List<GameplayEvent> EventList;

	private Transform GameplayMessageContainer;

	private Transform OtherMessageContainer;

	public List<KillStreak> listKillStreakEvent = new List<KillStreak>();

	public bool canGiveReward;

	public void GenerateRandomListKillStreakEvent(int num)
	{
		for (int i = 0; i < num; i++)
		{
			AddKillStreak("RewardMessages " + i, (RewardMessages)Random.Range(0, 14));
		}
	}

	public void ClearKillStreak()
	{
		listKillStreakEvent.Clear();
	}

	public void AddKillStreak(string nameKillStreak, RewardMessages reward)
	{
		KillStreak killStreak = new KillStreak();
		if (listKillStreakEvent.Count == 0)
		{
			killStreak.count = 1;
			killStreak.Number = 1;
			killStreak.nameStreak = nameKillStreak;
			killStreak.reward = reward;
			listKillStreakEvent.Add(killStreak);
			return;
		}
		bool flag = true;
		for (int i = 0; i < listKillStreakEvent.Count; i++)
		{
			if (listKillStreakEvent[i].reward == reward)
			{
				listKillStreakEvent[i].Number++;
				listKillStreakEvent[i].nameStreak = nameKillStreak;
				flag = false;
			}
		}
		if (flag)
		{
			killStreak.count = listKillStreakEvent.Count + 1;
			killStreak.Number = 1;
			killStreak.nameStreak = nameKillStreak;
			killStreak.reward = reward;
			listKillStreakEvent.Add(killStreak);
		}
	}

	public bool CanGiveReward()
	{
		if (!settings.offlineMode && PhotonNetwork.room != null && GameController.instance != null)
		{
			canGiveReward = string.IsNullOrEmpty(PhotonNetwork.room.customProperties["pass"].ToString()) && GameController.instance.listPlayers.Count > 1;
			return canGiveReward;
		}
		canGiveReward = true;
		return canGiveReward;
	}

	private void Awake()
	{
		instance = this;
		EventList = new List<GameplayEvent>();
		GameObject gameObject = GameObject.FindGameObjectWithTag("GameplayMessagesContainer");
		GameObject gameObject2 = GameObject.FindGameObjectWithTag("OtherMessagesContainer");
		if ((bool)gameObject)
		{
			GameplayMessageContainer = gameObject.transform;
		}
		if ((bool)gameObject2)
		{
			OtherMessageContainer = gameObject2.transform;
		}
	}

	public void Reset(bool resetPoints)
	{
		EventList.Clear();
		if (resetPoints && (bool)GameController.instance && (bool)GameController.instance.playerScript)
		{
			GameController.instance.playerScript.lastKillerID = -1;
		}
	}

	public void AddToRewardQueue(GameplayEvent gameplayEvent, bool tryShowMessage = false)
	{
		if (TutorialManager.TuturialWorking())
		{
			return;
		}
		if (gameplayEvent.rewardType == GameplayRewardType.GameplayEventMessage)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Event_Complete", gameplayEvent.name);
			FlurryWrapper.LogEvent(FlurryWrapper.EV_EVENT_COMPLETE, dictionary);
		}
		if (CanGiveReward())
		{
			if (settings.offlineMode)
			{
				SessionReputationManager.Instance.AddReputation(gameplayEvent.offlineREP);
			}
			else
			{
				SessionReputationManager.Instance.AddReputation(gameplayEvent.sendboxREP);
			}
		}
		if (gameplayEvent.rewardType != 0)
		{
			EventList.Add(gameplayEvent);
		}
		if (gameplayEvent.rewardType == GameplayRewardType.GameplayEventMessage)
		{
			AddKillStreak(gameplayEvent.name, gameplayEvent.rewardMessagesSettings);
		}
		if (tryShowMessage)
		{
			TryShowMessage();
		}
	}

	public void TryShowMessage()
	{
		if (RewardMessage.instance == null)
		{
			ShowNextMessage();
		}
	}

	public void ShowNextMessage()
	{
		GameplayEvent gameplayEvent = new GameplayEvent();
		gameplayEvent.pvpXP = -100000;
		gameplayEvent.pvpREP = -100000f;
		gameplayEvent.sendboxXP = -100000;
		gameplayEvent.sendboxREP = -100000f;
		gameplayEvent.offlineXP = -100000;
		gameplayEvent.offlineREP = -100000f;
		int num = -1;
		foreach (GameplayEvent @event in EventList)
		{
			if (settings.offlineMode)
			{
				if (@event.offlineXP >= gameplayEvent.offlineXP)
				{
					gameplayEvent = @event;
					num = EventList.IndexOf(@event);
				}
			}
			else if (@event.pvpXP >= gameplayEvent.pvpXP)
			{
				gameplayEvent = @event;
				num = EventList.IndexOf(@event);
			}
		}
		if (num <= -1)
		{
			return;
		}
		if (GameplayMessageContainer != null)
		{
			GameObject gameObject = null;
			switch (gameplayEvent.rewardType)
			{
			case GameplayRewardType.GameplayEventMessage:
				if ((bool)GameplayMessageContainer)
				{
					gameObject = Object.Instantiate(GameplayEventMessagePrefab);
					gameObject.GetComponent<GameplayEventMessage>().SetMessageInfo(gameplayEvent);
					gameObject.transform.SetParent(GameplayMessageContainer);
				}
				else
				{
					BCWDebug.LogError("No GameplayMessageContainer!");
				}
				break;
			case GameplayRewardType.NewReputationLevel:
				if ((bool)OtherMessageContainer)
				{
					gameObject = Object.Instantiate(NewReputationLevelMessagePrefab);
					gameObject.GetComponent<NewReputationLevelMessage>().SetMessageInfo(gameplayEvent);
					gameObject.transform.SetParent(OtherMessageContainer);
				}
				else
				{
					BCWDebug.LogError("No OtherMessageContainer!");
				}
				break;
			case GameplayRewardType.HiddenCoinCollected:
				if ((bool)OtherMessageContainer)
				{
					gameObject = Object.Instantiate(HiddenCoinCollectedMessagePrefab);
					gameObject.GetComponent<HiddenCoinCollectedMessage>().SetMessageInfo(gameplayEvent);
					gameObject.transform.SetParent(OtherMessageContainer);
				}
				else
				{
					BCWDebug.LogError("No OtherMessageContainer!");
				}
				break;
			}
			if (gameObject != null)
			{
				gameObject.transform.localScale = Vector3.one;
				gameObject.transform.localPosition = new Vector3(0f, 0f, 0f);
			}
			else
			{
				BCWDebug.LogError("Message is null!");
			}
		}
		else
		{
			BCWDebug.LogError("GameplayEventMessagePanel not foung! Place RewardMessagesPanel prefab to UIRoot and restart.");
		}
		EventList.RemoveAt(num);
	}

	public void ShowOverheadTextMessage(Transform target, string messageText)
	{
	}
}
